package view.listeners;

import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;

import model.Constants;
import view.views.dialogContentViews.AddSimpleIPContentPanel;
import controller.Calculator;

public class BinaryToDecimalListener extends FocusAdapter {


	private final AddSimpleIPContentPanel content;
	private String text;
	private final int i;

	public BinaryToDecimalListener(
			AddSimpleIPContentPanel addSimpleIPContentPanel, int i) {
		content = addSimpleIPContentPanel;
		this.i = i;
	}

	@Override
	public void focusLost(FocusEvent e) {
		text = content.getBinaryIpTextArea().getTextList().get(i).getText();
		if (text.equals(Constants.EMPTY) || text == null) {
			content.getIpTextArea().getTextList().get(i)
					.setText(Constants.EMPTY);

		} else {
			int decimal = Calculator.getInstance().convertBinaryToDecimal(
					text);
			content.getIpTextArea()
					.getTextList()
					.get(i)
					.setText(Integer.toString(decimal));
		}
	}

}
